package com.star.controller.admin;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.star.pojo.Blog;
import com.star.pojo.Type;
import com.star.pojo.User;
import com.star.service.BlogService;
import com.star.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import java.util.List;

@Controller
@RequestMapping("/admin/blogs")
public class BlogController {

    @Resource
    private BlogService blogService;

    @Autowired
    private TypeService typeService;

    /**
     * 博客列表
     */
    @GetMapping
    public String blogs(@RequestParam(value = "pn",defaultValue = "1") Integer pn, Model model){

        List<Type> types = typeService.getAllType();

        model.addAttribute("types",types);

        //后面紧跟着的一个查询就是分页查询，每页5条记录
        PageHelper.startPage(pn,5);
        //这个查询语句就是分页查询
        List<Blog> blogs=blogService.BlogList();
        //将分页查询结果包装到分页插件提供的PageInfo类中，前台通过pageInfo即可获得所有的分页信息
        PageInfo pageInfo=new PageInfo(blogs,5);

        model.addAttribute("pageInfo",pageInfo);

        return "admin/blogs";
    }

    /**
     * 去新增博客页面
     */
    @GetMapping("/addBlog")
    public String addBlog(Model model) {

        model.addAttribute("types", typeService.getAllType());
        model.addAttribute("blog", new Blog());

        return "admin/blogs-input";
    }


    /***
     *  编写(新增)博客
     */
    @PostMapping("/addBlog")
    public String addBlog(Blog blog, RedirectAttributes attributes, HttpSession session) {

        User user= (User) session.getAttribute("user");  //获取当前登录的用户信息

        Blog blog2=new Blog();

        //设置blog的userId
        blog2.setUserId(user.getId());
        //设置blog的typeId
        blog2.setTypeId(blog.getType().getId());

        //新增博客
        int i = blogService.saveBlog(blog2);

        if (i==0){
            attributes.addFlashAttribute("message","新增失败");
        }else {
            attributes.addFlashAttribute("message","新增成功");
        }

        return "redirect:/admin/blogs";
    }

    /***
     * 去博客编辑页面
     */
    @GetMapping("/editBlog/{id}")
    public String editBlog(@PathVariable Long id, Model model) {

        model.addAttribute("types", typeService.getAllType());

        model.addAttribute("blog",blogService.getBlogById(id));

        return "admin/blogs-input";
    }


    /***
     * 修改博客
     */
    @PostMapping("/editBlog/{id}")
    public String editBlog(@Valid Blog blog, RedirectAttributes attributes, HttpSession session) {

        User user= (User) session.getAttribute("user");  //获取当前登录的用户信息

        blog.setUserId(user.getId());

        int i = blogService.updateBlog(blog);
        if (i==0){
            attributes.addFlashAttribute("message","修改失败");
        }else {
            attributes.addFlashAttribute("message","修改成功");
        }

        return "redirect:/admin/blogs";
    }


    /**
     *  删除博客
     */
    @GetMapping("/deleteBlog/{id}")
    public String deleteBlog(@PathVariable Long id, RedirectAttributes attributes) {

        blogService.deleteBlog(id);
        attributes.addFlashAttribute("message", "删除成功");

        return "redirect:/admin/blogs";
    }





}
